<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>主生产计划</title>
    <script src="https://www.itxst.com/package/ag-grid/ag-grid-community.js"></script>
    <script src="vue.js"></script>
    <link href="https://www.itxst.com/package/ag-grid/styles/ag-grid.css" rel="stylesheet"/>
</head>
<body>
    <form>
    
    
    <input type="button" value="生成表单" onclick="getData()" />
    
    <div id="myGrid" style="width: 100%;height: 500px;" class="ag-theme-balham"></div>
    
    <script>



    
var columnDefs = [
        { headerName: '类别', field: 'a' , maxWidth:120},
        { headerName: '期初', field: 'a0' , maxWidth:120},
        
];

//对应的数据
var data = [
    { a: '预测量0',a0:'',a1:'',a2:'',a3:'',a4:'',a5:'',a6:'',a7:'',a8:'',a9:'',a10:'',a11:'',a12:'',a13:'',a14:'',a15:'',a16:'',a17:'',a18:'',a19:'',a20:'',},
    { a: '订单量1',a0:'',a1:'',a2:'',a3:'',a4:'',a5:'',a6:'',a7:'',a8:'',a9:'',a10:'',a11:'',a12:'',a13:'',a14:'',a15:'',a16:'',a17:'',a18:'',a19:'',a20:'',},
    { a: '毛需求量2',a0:'',a1:'',a2:'',a3:'',a4:'',a5:'',a6:'',a7:'',a8:'',a9:'',a10:'',a11:'',a12:'',a13:'',a14:'',a15:'',a16:'',a17:'',a18:'',a19:'',a20:'',},
    { a: '计划接收量3',a0:'',a1:'',a2:'',a3:'',a4:'',a5:'',a6:'',a7:'',a8:'',a9:'',a10:'',a11:'',a12:'',a13:'',a14:'',a15:'',a16:'',a17:'',a18:'',a19:'',a20:'',},
    { a: '预计库存量（前）4',a0:'',a1:'',a2:'',a3:'',a4:'',a5:'',a6:'',a7:'',a8:'',a9:'',a10:'',a11:'',a12:'',a13:'',a14:'',a15:'',a16:'',a17:'',a18:'',a19:'',a20:'',},
    { a: '预计库存量（后）5',a0:'',a1:'',a2:'',a3:'',a4:'',a5:'',a6:'',a7:'',a8:'',a9:'',a10:'',a11:'',a12:'',a13:'',a14:'',a15:'',a16:'',a17:'',a18:'',a19:'',a20:'',},
    { a: '净需求量6',a0:'',a1:'',a2:'',a3:'',a4:'',a5:'',a6:'',a7:'',a8:'',a9:'',a10:'',a11:'',a12:'',a13:'',a14:'',a15:'',a16:'',a17:'',a18:'',a19:'',a20:'',},
    { a: '计划产出量7',a0:'',a1:'',a2:'',a3:'',a4:'',a5:'',a6:'',a7:'',a8:'',a9:'',a10:'',a11:'',a12:'',a13:'',a14:'',a15:'',a16:'',a17:'',a18:'',a19:'',a20:'', },
    { a: '计划投入量8',a0:'',a1:'',a2:'',a3:'',a4:'',a5:'',a6:'',a7:'',a8:'',a9:'',a10:'',a11:'',a12:'',a13:'',a14:'',a15:'',a16:'',a17:'',a18:'',a19:'',a20:'', },
    { a: '可供销售量9',a0:'',a1:'',a2:'',a3:'',a4:'',a5:'',a6:'',a7:'',a8:'',a9:'',a10:'',a11:'',a12:'',a13:'',a14:'',a15:'',a16:'',a17:'',a18:'',a19:'',a20:'', },
];

//ag-grid设置
var gridOptions = {
    columnDefs: columnDefs,
    rowData: data,
    onGridReady: function () {
        //创建完成执行的事件
        gridOptions.api.sizeColumnsToFit();
    },
    defaultColDef: { 
        enablePivot: true,
        enableValue: true,
        
        filter: true  ,//开启刷选
        editable: true,//单元表格是否可编辑
    },
    pagination: true,  //开启分页（前端分页）
    paginationAutoPageSize: true, //根据网页高度自动分页（前端分页）

};
//初始化agGrid
document.addEventListener("DOMContentLoaded", function () {
    var eGridDiv = document.querySelector('#myGrid');
    new agGrid.Grid(eGridDiv, gridOptions);
});
//新增地址列



let jsjg = document.getElementById('jsjg');
let arr = location.search.split("&",8);

data[5].a0 = arr[7];
var num = arr[3];

for (var i=1;i<=num;i++)
{ 
    columnDefs.push({ headerName:i, field: 'a'+ i ,maxWidth:80});
    
};
gridOptions.api.setColumnDefs(columnDefs);
    //调整表格大小自适应
gridOptions.api.sizeColumnsToFit();


function getData(){
    var selRow = gridOptions.api.getSelectedRows();
    for (var j=1;j<=num;j++){
        if(j<=arr[1]){
            data[2]['a'+j]=data[1]['a'+j]
        }
        else if(j>arr[2]){
            data[2]['a'+j]=data[0]['a'+j]*1
        }
        else if(arr[1]<j<=arr[2]){
            if(data[0]['a'+j]*1>=data[1]['a'+j]*1){
                data[2]['a'+j]=data[0]['a'+j]
            }else if(data[0]['a'+j]*1<data[1]['a'+j]*1){
                data[2]['a'+j]=data[1]['a'+j]
            }
        }
        
    }//遇事不决，就*1
    for (var i=1;i<=num;i++){
        if(i==1){
            data[5]['a1']=data[5]['a0']*1+data[3]['a1']*1-data[2]['a1']*1//预计库存后
            data[4]['a1']=data[5]['a0']*1-data[2]['a1']*1//预计库存前
            let n1 = data[2]['a1']*1-data[5]['a0']*1-data[3]['a1']*1//净需求data[6]['a1']=
            if(n1<0){
                data[6]['a1']=0
            } 
        }
        else{
            let n = i-1
            data[4]['a'+i]=data[5]['a'+n]*1-data[2]['a'+i]*1//预计库存前
            
            let n3 =  data[4]['a'+i]*1+data[3]['a'+i]*1
            
            if(n3>0){
                if(n3>=arr[6]){
                    data[6]['a'+i]=0
                    data[7]['a'+i]=0
                }else if(n3<arr[6]){
                    data[6]['a'+i]=arr[6]*1-n3
                    let n4 = data[6]['a'+i]*1
                    let s = i-arr[4]
                if(0<n4<arr[5]){
                    data[7]['a'+i]=arr[5]
                    data[8]['a'+s]=data[7]['a'+i]
                }else if(arr[5]<n4){
                    data[7]['a'+i]=Math.ceil(n4/arr[5])*arr[5]
                    data[8]['a'+s]=data[7]['a'+i]
                }
                }
            }else if(n3<0){
                data[6]['a'+i]=-n3*1+arr[6]*1
                let n4 = data[6]['a'+i]*1
                let s = i-arr[4]
            if(0<n4<arr[5]){
                data[7]['a'+i]=arr[5]
                data[8]['a'+s]=data[7]['a'+i]
            }else if(arr[5]<n4){
                data[7]['a'+i]=Math.ceil(n4/arr[5])*arr[5]
                data[8]['a'+s]=data[7]['a'+i]
            }
            }
            
            
            data[5]['a'+i]=data[4]['a'+i]*1+data[7]['a'+i]*1
            // if(n1<0){
            //     data[6]['a'+i]=0
            // }else if(n1>0){
            // data[6]['a'+i]=data[2]['a'+i]*1-data[5]['a'+n]*1-data[3]['a'+i]*1+arr[6]*1//净需求
            // }
            
            
            // if(data[6]['a'+i]=0){
            //     data[7]['a'+i]=0
                
            // }else{
            //     let m = data[6]['a'+i]*1
            // if(0<m/arr[5]<=1){
            //     data[7]['a'+i]=arr[5]
            // }else if(m/arr[5]>1){
            //     let n2 = Math.ceil(m/arr[5])
            //     data[7]['a'+i] = n2*arr[5]
            // }  
            //     }
            // data[5]['a'+i]=data[5]['a'+n]*1+data[3]['a'+i]*1-data[2]['a'+i]*1+data[7]['a'+i]*1//预计库存后
          
     
        }
    }
    gridOptions.api.updateRowData(               
 	 {      
 	 	update: data               
 	 });
}
gridOptions.api.setRowData(data);
   
    </script>
</body>
</html>